package com.example.framework.bmob.bmobManager;


import android.content.Context;

import com.example.framework.bmob.User;

import java.io.File;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * @author:0.2℃
 * @date： 2022-04-02 20:32
 * @Description ：Bmob 管理类
 */
public class BmobManager {
    private static volatile BmobManager instance = null;
    private static final String BMOB_SDK_ID = "744eceda7a44dd178ae01ccb5e1ba4d2";
//    private static final String BMOB_SDK_ID = "f8efae5debf319071b44339cf51153fc";

    private BmobManager() {

    }

    public static BmobManager getInstance() {
        if (instance == null) {
            synchronized(BmobManager.class) {
                if (instance == null) {
                    instance = new BmobManager();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化Bmob
     * @param mContext
     */
    public void initBmob(Context mContext) {
        Bmob.initialize(mContext, BMOB_SDK_ID);
    }

    /**
     * 获得本地对象
     * @return
     */
    public User getUser () {
        return BmobUser.getCurrentUser(User.class);
    }

    /**
     * 发送验证码
     * @param phone     手机号
     * @param listener  回调
     */
    public void requestSMS(String phone, QueryListener<Integer> listener) {
        BmobSMS.requestSMSCode(phone, "", listener);
    }

    /**
     * 通过手机号码注册或登录
     * @param phone
     * @param code
     * @param listener
     */
    public void signOrLoginByMobilePhone(String phone, String code, LogInListener<User> listener) {
        BmobUser.signOrLoginByMobilePhone(phone, code, listener);
    }

    /**
     * 判断是否登录了Bmob
     * @return
     */
    public boolean isLogin() {
        return BmobUser.isLogin();
    }

    /**
     * 上传头像和昵称
     * @param nickname
     * @param file
     * @param listener
     */
    public void uploadFirstPhoto(String nickname, File file, final OnUploadPhotoListener listener) {
        // 1. 上传文件，拿到地址
        // 2. 更新用户信息
        final User user = getUser();
        final BmobFile bmobFile = new BmobFile(file);
        bmobFile.uploadblock(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    // 上传成功
                    user.setNickName(nickname);
                    user.setPhoto(bmobFile.getFileUrl());

                    user.setTokenNickName(nickname);
                    user.setTokenPhoto(bmobFile.getFileUrl());

                    // 更新用户信息
                    user.update(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                listener.onUploadDone();
                            } else {
                                listener.onUploadFail(e);
                            }
                        }
                    });
                } else {
                    listener.onUploadFail(e);
                }
            }
        });
    }

    public interface OnUploadPhotoListener {
        void onUploadDone();

        void onUploadFail(BmobException e);
    }


    /**
     * 通过电话号码查询
     */
    public void queryFriendFromPhone(String phone, FindListener<User> listener) {
        baseQuery("mobilePhoneNumber", phone, listener);
    }

    /**
     * 查询所有的用户
     * @param listener
     */
    public void queryAllUser(FindListener<User> listener) {
        BmobQuery<User> query = new BmobQuery<>();
        query.findObjects(listener);
    }

    /**
     * 查询的基类
     */
    public void baseQuery(String key, String val, FindListener<User> listener) {
        BmobQuery<User> query = new BmobQuery<>();
        query.addWhereEqualTo(key, val);
        query.findObjects(listener);
    }

}
